Conditions | 11 |
Total Lines | 93 |
Code Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Boost.run often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import { AppSettings } from '@/lib/AppSettings'; |
||
125 | |||
126 | public async run(repository: Repository, args: any[] = []) { |
||
127 | this.repository = repository; |
||
128 | |||
129 | this.repository.initGitListeners(this.runId); |
||
130 | |||
131 | const listener = eventbus.on(GIT_FILE_ADDED_EVENT, ({ repository, files, runId }) => { |
||
132 | if (runId !== this.runId || this.repository?.fullRepositoryName() !== `${repository.owner}/${repository.name}`) { |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | this.changedFiles.push(...files); |
||
137 | }); |
||
138 | |||
139 | // changes to historyItem properties will be reflected in the saved history automatically |
||
140 | // as createEntry returns a proxy object |
||
141 | const historyItem: BoostHistoryItem = this.codeBoost.historyManager.createEntry({ |
||
142 | run_id: this.runId, |
||
143 | boost: this.id, |
||
144 | version: this.version, |
||
145 | repository: repository.fullRepositoryName(), |
||
146 | started_at: new Date().toISOString(), |
||
147 | finished_at: null, |
||
148 | state: BoostHistoryItemState.RUNNING, |
||
149 | pull_request: null, |
||
150 | }); |
||
151 | |||
152 | if (!this.canRunOnRepository(repository)) { |
||
153 | historyItem.state = BoostHistoryItemState.SKIPPED; |
||
154 | historyItem.finished_at = new Date().toISOString(); |
||
155 | |||
156 | this.log(`Cannot run on ${repository.fullRepositoryName()}`); |
||
157 | this.log('Done.'); |
||
158 | return false; |
||
159 | } |
||
160 | |||
161 | const params = await this.createScriptHandlerParameters(args, historyItem); |
||
162 | |||
163 | const catchErrors = async (callBack: CallableFunction, args: any = []) => { |
||
164 | try { |
||
165 | return await await callBack(...args); |
||
166 | } catch (e) { |
||
167 | hasError = true; |
||
168 | this.log(e); |
||
169 | return false; |
||
170 | } |
||
171 | }; |
||
172 | |||
173 | const handleDryRun = async (callback: CallableFunction, dryRunMessage: string) => { |
||
174 | if (!this.appSettings.dry_run) { |
||
175 | return await callback(); |
||
176 | } |
||
177 | |||
178 | this.log(`[dry run] ${dryRunMessage}`); |
||
179 | return null; |
||
180 | }; |
||
181 | |||
182 | let hasError = false; |
||
183 | |||
184 | if (!this.appSettings.use_pull_requests) { |
||
185 | this.pullRequest.branch = await repository.defaultBranch(); |
||
186 | } |
||
187 | |||
188 | if (this.appSettings.use_pull_requests) { |
||
189 | await this.updatePullRequestBranchName(); |
||
190 | await this.checkoutPullRequestBranch(); |
||
191 | } |
||
192 | |||
193 | await catchErrors(async () => { |
||
194 | await this.runInitializationScript(params); |
||
195 | await this.runScripts(params); |
||
196 | }); |
||
197 | |||
198 | if (!hasError && this.changedFiles.length > 0) { |
||
199 | await catchErrors(async () => { |
||
200 | const remote = this.appSettings.use_forks ? 'fork' : 'origin'; |
||
201 | await handleDryRun( |
||
202 | async () => await repository.git.push(remote, this.pullRequest.branch), |
||
203 | `push branch ${this.pullRequest.branch} to ${remote}`, |
||
204 | ); |
||
205 | }); |
||
206 | |||
207 | await catchErrors(async () => { |
||
208 | handleDryRun(async () => { |
||
209 | await this.handlePullRequestCreation({ repository, historyItem }); |
||
210 | }, 'create pull request'); |
||
211 | }); |
||
212 | } |
||
213 | |||
214 | historyItem.finished_at = new Date().toISOString(); |
||
215 | historyItem.state = hasError ? BoostHistoryItemState.FAILED : BoostHistoryItemState.SUCCEEDED; |
||
216 | |||
217 | listener.off(GIT_FILE_ADDED_EVENT, () => {}); |
||
218 | } |
||
337 |